<?php
class CookieException extends Exception {};

/**
 * Description of Cookie
 *
 * @author Martin Grossmann <snake.aas@gmail.com>
 * @version 1.0
 * @package includes
 */
class Cookie {
  
    
  private static $version = 1;
  private static $glue = ':';
  
  private $expiration = 0;
  
  public function __construct() {
   
  }


  public function __get($name) {

    if (!isset($_COOKIE[$name])) return null;
    
    list($Cversion, $Ctime, $Cvalue) = explode(self::$glue, base64_decode($_COOKIE[$name]));
    
    if (!$Cversion || !$Ctime || !$Cvalue) throw new CookieException(T_('Malformed cookie'));
    
    if ($Cversion != self::$version) throw new CookieException(T_('Cookie version mismatch'));
    
    //if (time() - $time > $this->expiration)
    return $Cvalue;
  }
  
  public function __set($name, $value) {
   
    if ($value !== null) {    
      $parts = array(self::$version, time(), $value);
      $partsAsString = implode(self::$glue, $parts);
      $value = base64_encode($partsAsString);
    } else 
      $value = '';
    
    setcookie($name, $value, $this->expiration, '', '', false, true);
  }

public function setExpiration($value) {
  if ($value == null) 
    $this->expiration = time() - 3600;
  else
    $this->expiration = time() + $value;
  return $this;
} 
  
}

?>
